//
//  SSZProvinceViewController.m
//  QuickCure
//
//  Created by 周洋 on 16/9/13.
//  Copyright © 2016年 haoge. All rights reserved.
//

#import "SSZProvinceViewController.h"
#import "ProvinceModel.h"
#import "Masonry.h"

@interface SSZProvinceViewController () <UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, strong) NSArray *provinceArray;

@end

@implementation SSZProvinceViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //创建tableView
    [self createTableView];
    
    //导航栏title
    self.navigationItem.title = [NSString stringWithFormat:@"选择当前%@位置", self.cellNumber == -1? @"省份":@"城市"];
}

- (void)createTableView {
    
    self.navigationController.navigationBar.translucent = NO;
    
    [self.view addSubview:self.tableView];
    
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.edges.equalTo(self.view);
    }];
    
    //注册cell
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"province"];
}

#pragma mark - Table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    //如果是-1，返回provinceArray的长度
    if(self.cellNumber == -1) {
        
        return self.provinceArray.count;
    }
    //不是-1，获取省份对应的城市的数组
    ProvinceModel *model = self.provinceArray[self.cellNumber];
    
    NSArray *cityArray = model.cities;
    
    return cityArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"province"];
    
    
    if(self.cellNumber == -1) {
        
        //如果是-1,得到省份数组,返回省份名称
        ProvinceModel *model = self.provinceArray[indexPath.row];
        cell.textLabel.text = model.name;
        return cell;
    }
    //不是-1,
    ProvinceModel *model = self.provinceArray[self.cellNumber];
    
    NSArray *cityArray = model.cities;
    
    cell.textLabel.text = cityArray[indexPath.row];
    return cell;
}

#pragma mark -- 点击cell
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    //如果是-1，点击cell调至城市列表
    if(self.cellNumber == -1) {
        
        SSZProvinceViewController *vc = [[SSZProvinceViewController alloc] init];
        
        vc.cellNumber = indexPath.row;
        
        [self.navigationController pushViewController:vc animated:YES];
    }
    else {
        
        ProvinceModel *model = self.provinceArray[self.cellNumber];
        
        NSArray *cityArray = model.cities;
        
        //如果不是-1，把选中的城市传给Home控制器(通知)
        NSNotification *nc = [[NSNotification alloc] initWithName:@"cityName" object:nil userInfo:@{@"name":cityArray[indexPath.row]}];
        [[NSNotificationCenter defaultCenter] postNotification:nc];
        
        //回到主界面
        [self.navigationController popToRootViewControllerAnimated:YES];
        
    }
    
}

//重写init，cellNubmer初始值为-1
-(instancetype)init {
    
    if(self = [super init]) {
        
        self.cellNumber = -1;
    }
    return self;
}
//懒加载
- (UITableView *)tableView {
    
    if(nil == _tableView) {
        
        _tableView = [[UITableView alloc] init];
        _tableView.delegate = self;
        _tableView.dataSource = self;
    }
    return _tableView;
}

- (NSArray *)provinceArray {
    
    if(nil == _provinceArray) {
        
        _provinceArray = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle]pathForResource:@"province.plist" ofType:nil]];
        
        NSMutableArray *nmArray = [NSMutableArray array];
        
        [_provinceArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            
            ProvinceModel *model = [ProvinceModel provinceWithDictionary:obj];
            
            [nmArray addObject:model];
        }];
        _provinceArray = nmArray;
    }
    return _provinceArray;
}

@end













